const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const app = express();

// 引入usr.js
const users = require("./routers/api/users");
const profiles = require("./routers/api/profiles");

// 数据库配置
const db = require("./config/keys").mongoURI;

// 使用body-parser中间件
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// 连接数据库
mongoose
  .connect(db)
  .then(() => {
    console.log("数据库连接成功!");
  })
  .catch((err) => console.log(err));

// passport 的初始化
app.use(passport.initialize());

// 配置passport
require("./config/passport")(passport);

app.get("/", (req, res) => {
  res.send("Hello World!");
});

// 使用routes
app.use("/api/users", users);
app.use("/api/profiles", profiles);

const port = process.env.PORT || 5000;

app.listen(port, () => {
  console.log(`服务器 运行 在 ${port} 端口`);
});
